<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../hy/common.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" href="${pageContext.request.contextPath }/pages/zc/css/customer.css">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>客户管理</title>
	<script type="text/javascript">
        $(function(){
            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();
            //2.初始化Button的点击事件
            var oButtonInit = new ButtonInit();
            oButtonInit.Init();

			$(".form_datetime").datetimepicker({
				/* 显示格式 */
				format: "yyyy-mm-dd",
				/* 完成关闭 */
				autoclose: true,
				/*  今天 时间按钮*/
				todayBtn: true,
				/*  今天的时间变黄色 */
				todayHighlight:true,
				/* 定位 */
				pickerPosition: "bottom-left",
				/* 最小值 */
				minuteStep: 10,
				/*  语言 */
				language: 'zh-CN',
				/* 分上午下午 */
				showMeridian: true,
				/* 分的所有 */
				minuteStep:true,
				/* 月视图 */
				startView: 2,
				/* 日期时间选择器所能够提供的最精确的时间选择视图 */
				minView: 2
			})
        });

        var TableInit = function () {
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init=function(){
                $("#tb_departments").bootstrapTable({
                    url: '${pageContext.request.contextPath }/trolley/queryTrolleyInfoList.do',//请求后台的url
                    method: 'get',                     	//请求方式（*）
                    toolbar: '#toolbar',                //工具按钮用哪个容器
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    sortable: false,                     //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber:1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [10, 20, 30],        		//可供选择的每页的行数（*）
                    clickToSelect: true,                //是否启用点击选中行
                    height: 550,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
                    detailView: false,                   //是否显示父子表
                    showExport: true,  //是否显示导出按钮
                    columns:[{
                        checkbox:true
                    },{
                        field : 'trolleyCode',
                        title : '台车编码'
                    },{
                        field : 'trolleyName',
                        title : '台车名称'
                    },{
                        field : 'trolleyStatus',
                        title : '台车状态',
                        formatter:function(value,row,index){ 
			            	var value="";
			            	if(row.trolleyStatus=="1"){
			            		value = "占用";
			            	}else if(row.trolleyStatus=="0"){
			            		value = "空闲";
			            	}
								return value;
			            }
                    },{
                        field : 'remark01',
                        title : '录入人'
                    },{
                        field : 'remark02',
                        title : '录入时间'
                    }]
                });
            }
            //得到查询的参数
            oTableInit.queryParams = function (params) {
                var temp = {
                    limit : params.limit, 					//页面大小
                    page : params.offset/params.limit + 1, 	//页码
                    trolleyName : $("#trolleyName2").val()
                };
                return temp;
            };
            return oTableInit;
        }
        var ButtonInit = function () {
            var oInit = new Object();
            oInit.Init = function () {
                //初始化页面上面的按钮事件
            };
            return oInit;
        };

        //添加
        function doAdd() {
            //打开模态框前进行重置
            $("#addForm")[0].reset();
            $("#addModal").modal(open);
            //添加数据+检验
            $(function() {
                var layerTips = parent.layer === undefined ? layui.layer : parent.layer
                $("#addModal").bootstrapValidator({
                    feedbackIcons : {
                        valid : 'glyphicon glyphicon-ok',
                        invalid : 'glyphicon glyphicon-remove',
                        validating : 'glyphicon glyphicon-refresh'
                    },
                    fields : {
                    	trolleyCode : {
                            validators : {
                                notEmpty : {
                                    message : '台车编码不能为空'
                                },
                            },
                        },
                        trolleyName : {
                            validators : {
                                notEmpty : {
                                    message : '台车名称不能为空'
                                },
                            },
                        },
                        /* linkman : {
                            validators : {
                                notEmpty : {
                                    message : '台车状态不能为空'
                                },
                            },
                        } */
                    },
                }).on('success.form.bv',function() {
                    $.ajax({
                        type : 'post',
                        url : '${pageContext.request.contextPath }/trolley/addTrolleyInfo.do',
                        data : $("#addForm").serialize(),
                        async:false,
                        success : function(data) {
                            if (data > 1) {
                                toastr.success("新增成功！");
                                setTimeout(function(){
                                    window.location.reload();
                                },2000);
                                //$('#addModal').modal('hide');
                            }else {
                                toastr.warning("新增失败");
                                setTimeout(function(){
                                    window.location.reload();
                                },1000);
                            }
                        }
                    });
                });
            });
        }

        //删除
        function doDel() {
            var rowData= $('#tb_departments').bootstrapTable('getSelections');
            var idArr = new Array();
            for(var i = 0;i < rowData.length; i++) {
                idArr[i] = rowData[i].id;
            }
            if(rowData.length > 0) {
                //询问框
                Ewin.confirm({message : "确认要删除选择的数据吗？"}).on(
                    function(e) {
                        if (!e) {return;}
                        $.ajax({
                            type: "get",
                            url: '${pageContext.request.contextPath }/trolley/deleteTrolleyInfoById.do',
                            data:{"idArr" : idArr},
                            success: function(data){
                                if(data > 0) {
                                    toastr.success("删除成功！");
                                    setTimeout(function(){
                                        window.location.reload();
                                    },2000);
                                }
                                if(data == -1){
                                    toastr.warning('删除失败！');
                                    setTimeout(function(){
                                        window.location.reload();
                                    },2000);
                                }
                            }
                        });
                    });
            }else {
                toastr.warning('请至少选择一条数据');
            }
        }

        //查询
        function selectByOther() {
            $('#tb_departments').bootstrapTable('refresh');
        }
        //导出当前页
        function doExport() {
            window.location.href="${pageContext.request.contextPath }/customer/export.do?exportType=1";
        }
        //导出所有
        /* function exportAll() {
            window.location.href="${pageContext.request.contextPath }/customer/export.do?exportType=2";
		} */
        function exportAll() {
            Ewin.confirm({message : "确认要导出所有数据吗？"}).on(
                function(e) {
                    if (!e) {return;}
                    window.location.href="${pageContext.request.contextPath}/customer/export.do?exportType=2";
                })
        }

        //去修改
        function doEdit() {
            var rowData = $('#tb_departments').bootstrapTable('getSelections');
            if(rowData.length == 1) {
                $("#edit").modal(open);
                $('#id').val($.trim(rowData[0].id ));
                $('#trolleyCode1').val($.trim(rowData[0].trolleyCode));
                $('#trolleyName1').val($.trim(rowData[0].trolleyName));
                //$('#trolleyStatus1').val($.trim(rowData[0].trolleyStatus));
                $('#remark01').val($.trim(rowData[0].remark01));
                $('#remark02').val($.trim(rowData[0].remark02));
            }else {
                toastr.warning('请选择一条数据');
            }
        }

        //保存修改
        function saveEdit() {
            $(function() {
                $("#edit").bootstrapValidator({
                    feedbackIcons : {
                        valid : 'glyphicon glyphicon-ok',
                        invalid : 'glyphicon glyphicon-remove',
                        validating : 'glyphicon glyphicon-refresh'
                    },
                    fields : {
                        trolleyCode : {
                            validators : {
                                notEmpty : {
                                    message : '台车编码不能为空'
                                },
                            },
                        },
                        trolleyName : {
                            validators : {
                                notEmpty : {
                                    message : '台车名称不能为空'
                                },
                            },
                        },
                        trolleyStatus : {
                            validators : {
                                notEmpty : {
                                    message : '台车状态不能为空'
                                },
                            },
                        }
                    },
                }).on('success.form.bv',function() {
                    var id = $.trim($("#id").val());
                    var trolleyCode = $.trim($("#trolleyCode1").val());
                    var trolleyName = $.trim($("#trolleyName1").val());
                    var trolleyStatus = $.trim($("#trolleyStatus1").val());
                    var remark01 = $.trim($("#remark01").val());
                    var remark02 = $.trim($("#remark02").val());
                    $.ajax({
                        type: "get",
                        url: "${pageContext.request.contextPath }/trolley/updateTrolleyInfoById.do",
                        data: {id:id, trolleyCode:trolleyCode, trolleyName:trolleyName, trolleyStatus:trolleyStatus, remark01:remark01, remark02:remark02},
                        success: function (data) {
                            if(data > 0){
                                toastr.success("修改成功！");
                                setTimeout(function(){
                                    window.location.reload();
                                },1000);
                            }else {
                                toastr.warning("修改失败");
                                setTimeout(function(){
                                    window.location.reload();
                                },1000);
                            }
                        }
                    });
                });
            });
        }

        //关闭添加表单时清空数据
        function resets() {
            $('#addForm').data('bootstrapValidator').resetForm(true);
        }
        function douploadModal() {
            $('#uploadModal').modal(open);
            //清空表单
            document.getElementById("importForm").reset();
        }
        function uploadBtn() {
            var fileName = $("#fileInput").val();
            //alert(fileName);
            if (fileName.length > 0) {
                var formData = new FormData($("#importForm")[0]);
                $.ajax({
                    url: '${pageContext.request.contextPath}/customer/uplodeFile.do' ,
                    data: formData,
                    type:'post',
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        if (data == 1) {
                            toastr.success('文件导入成功（重复数据已过虑）！');
                            setTimeout(function(){
                                window.location.reload();
                            },3000);
                        }else{
                            toastr.warning('文件格式异常，文件导入失败！');
                        }
                    },
                    error: function (data) {
                        toastr.error("文件格式异常，导入的数据不符合导入条件");
                    }
                });
            }else{
                toastr.warning('上传文件为空，请选择要导入的文件！');
            }
        }
        //导出数据模板
        function doExportFile() {
            Ewin.confirm({message : "确认要导出数据模板吗？"}).on(
                function(e) {
                    if (!e) {
                        return;
                    }
                    window.location.href="${pageContext.request.contextPath}/customer/doExportFile.do";
                    $('#uploadModal').modal('hide');
            	}
            )
        }

        toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
	</script>
</head>
<body>
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="exampleModalLabel">
					<span style="font-size:13px">excel导入</span>
				</h4>
			</div>
			<div class="modal-body">
				<form id="importForm" class="form-horizontal">
					<div class="form-group">
						<label  class="col-sm-3 control-label"></label>
						<div class="col-sm-7">
							<button id="btn_delete" type="button" class="btn btn-default" onclick="doExportFile()">
								<span class="glyphicon glyphicon-export" aria-hidden="true"></span>导出数据模板
							</button>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-5 control-label">excel文件:</label>
						<div class="col-sm-5">
							<input id="fileInput" name="file" type="file" accept=".xls,.xlsx"/>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-3 col-sm-offset-7">
							<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
							<button type="submit"  name="submit"  class="btn btn-primary" id="ss" onclick="uploadBtn()">导入</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>


<div class="panel-body" style="padding-bottom:0px;">
	<div class="panel panel-default">
		<div class="panel-heading">查询条件</div>
		<div class="panel-body">
			<form id="formSearch" class="form-horizontal">
				<div class="form-group" style="margin-top: 15px">
					<label class="control-label col-sm-1" for="trolleyName">台车名称</label>
					<div class="col-sm-3">
						<input type="text" class="form-control" id="trolleyName2">
					</div>
					<div class="col-sm-4" style="margin-top: -30px;">
						<button type="button" style="margin-top: 29px" id="btn_query" class="btn btn-primary" onclick="selectByOther()" data-query-params="queryParams" data-query-params-type="limit">查询</button>
						<button id="qingkong" hidden="hidden">清空筛选</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div id="toolbar" class="btn-group">
		<button type="button" class="btn btn-default" onclick="doAdd()">
			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
		</button>
		<button type="button" class="btn btn-default" onclick="doEdit()">
			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
		</button>
		<button type="button" class="btn btn-default" onclick="doDel()">
			<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
		</button>
		<!--
		<button id="btn_export" type="button" class="btn btn-default" onclick="doExport()">
            <span class="glyphicon glyphicon-export" aria-hidden="true"></span>导出当前页
        </button>
		<button id="btn_import" type="button" class="btn btn-default" onclick="douploadModal()">
			<span class="glyphicon glyphicon-bullhorn" aria-hidden="true"></span>导入
		</button>
		<button id="btn_export" type="button" class="btn btn-default" onclick="exportAll()">
			<span class="glyphicon glyphicon-export" aria-hidden="true"></span>导出全部
		</button>
		<button id="btn_hide" type="button" class="btn btn-default" style="display: none;">
			<span class="glyphicon glyphicon-cancel" aria-hidden="true"></span>隐藏
		</button>
		-->
	</div>
	<table id="tb_departments"></table>
</div>
<%--新增--%>
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close" aria-hidden="true" onclick="resets()">&times;</button>
				<h4 class="modal-title" id="addModalLabel">新增台车信息</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" role="form" id="addForm" method="post">
					<div class="form-group">
						<label for="trolleyCode" class="col-sm-3 control-label">台车编码:</label>
						<div class="col-sm-8" style="width: 385px;">
							<input type="text" id="trolleyCode" name="trolleyCode" class="form-control" data-bv-notempty="true">
							<span class="xing"></span>
						</div>
					</div>
					<div class="form-group">
						<label for="trolleyName" class="col-sm-3 control-label">台车名称:</label>
						<div class="col-sm-8" style="width: 385px;">
							<input type="text" id="trolleyName" name="trolleyName" class="form-control" data-bv-notempty="true">
							<span class="xing"></span>
						</div>
					</div>
					<%--<div class="form-group">
						<label for="trolleyStatus" class="col-sm-3 control-label">台车状态:</label>
						<div class="col-sm-7" style="width: 386px">
							&lt;%&ndash;<input type="text" id="trolleyStatus" name="trolleyStatus" class="form-control" style="width: 85%;" placeholder="请输入台车状态..." data-bv-notempty="true">&ndash;%&gt;
							<select id="trolleyStatus" class="selectpicker show-tick form-control">
								<option value="0" selected>空闲</option>
								<option value="1">使用中</option>
							</select>
						</div>
					</div>--%>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal" onclick="resets()">关闭</button>
						<button type="submit" class="btn btn-primary">保存</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
<!-- 修改 -->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="updateModalLabel" aria-hidden="true"
	 data-backdrop="static" data-keyboard="false">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="updateModalLabel">修改台车信息</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" role="form">
					<div class="form-group">
						<label for="trolleyCode" class="col-sm-3 control-label">台车编码:</label>
						<div class="col-sm-8"  style="width: 385px;">
							<input type="text" id="trolleyCode1" name="trolleyCode" class="form-control"  data-bv-notempty="true">
							<span class="xing"></span>
						</div>
						<input type="hidden" id="id" name="id">
					</div>
					<div class="form-group">
						<label for="trolleyName" class="col-sm-3 control-label">台车名称:</label>
						<div class="col-sm-8"  style="width: 385px;">
							<input type="text" id="trolleyName1" name="trolleyName" class="form-control" data-bv-notempty="true">
							<span class="xing"></span>
						</div>
					</div>
					<%--<div class="form-group">
						<label for="trolleyStatus" class="col-sm-3 control-label">台车状态:</label>
						<div class="col-sm-9" style="width: 386px">
							&lt;%&ndash;<input type="text" id="trolleyStatus1" name="trolleyStatus" class="form-control" style="width: 85%;" placeholder="请输入联系人..." data-bv-notempty="true">&ndash;%&gt;
							<select id="trolleyStatus1" class="selectpicker show-tick form-control">
								<option value="0" selected>空闲</option>
								<option value="1">使用中</option>
							</select>
						</div>
					</div>--%>
					<div class="form-group">
						<label for="remark01" class="col-sm-3 control-label">录入人:</label>
						<div class="col-sm-9">
							<input type="text" id="remark01" name="remark01" class="form-control" style="width: 85%;" placeholder="请输入录入人..." data-bv-notempty="true">
						</div>
					</div>
					<div class="form-group">
						<label for="remark02" class="col-sm-3 control-label">录入时间:</label>
						<div class="col-sm-9" style="width: 386px">
							<%--<input type="text" id="remark02" name="remark02" class="form-control" style="width: 85%;" placeholder="请输入录入时间..." data-bv-notempty="true">--%>
							<input type="text" name="remark02" id="remark02" placeholder="yyyy-MM-dd" readonly="readonly" class="form_datetime form-control" data-bv-notempty="true">
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal" onclick="resets()">关闭</button>
						<button type="submit" class="btn btn-primary" onclick="saveEdit()">保存</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
</body>
</html>